package com.zhuoli.service.springboot.redis.repository.repository;

import com.zhuoli.service.redis.client.template.redis.RedisTemplate;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

@Component
@AllArgsConstructor
@Slf4j
public class RedisStringRepository {

    private RedisTemplate redisTemplate;

    /*通过key获取对应地value*/
    public String getString(String key) {
        return redisTemplate.execute(jedis -> jedis.get(key));
    }

    /*set key-value对*/
    public String setKey(String key, String value) {
        return redisTemplate.execute(jedis -> jedis.set(key, value));
    }

    /*为给定 key 设置生存时间，当 key 过期时(生存时间为 0 )，它会被自动删除*/
    public Long setKeyExpire(String key, Integer expireTime) {
        return redisTemplate.execute(jedis -> jedis.expire(key, expireTime));
    }

    /*set key-value对，同时设置过期时间*/
    public String setKeyWithExpire(String key, Integer ddl, String value){
        return redisTemplate.execute(jedis -> jedis.setex(key, ddl, value));
    }

    /*将key中储存的数字值加1*/
    public Long incr(String key) {
        return redisTemplate.execute(jedis -> jedis.incr(key));
    }

    /*将key中储存的数字值减1*/
    public Long decr(String key) {
        return redisTemplate.execute(jedis -> jedis.decr(key));
    }

    /*从当前数据库中随机返回(不删除)一个key*/
    public String randomKey() {
        return redisTemplate.execute(Jedis::randomKey);
    }

    /*删除给定的key*/
    public Long delete(String key) {
        return redisTemplate.execute(jedis -> jedis.del(key));
    }
}

